package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAODatabaseStructure;
import br.ufrj.ppgi.greco.model.dao.DAOProperty;
import br.ufrj.ppgi.greco.model.dao.DAOType;
import br.ufrj.ppgi.greco.model.entity.EntityProperty;
import br.ufrj.ppgi.greco.model.entity.EntityType;



@Resource
public class ConfigController
{
	private Result result;
	private DAODatabaseStructure dbs;
	private DAOProperty dp;
	private DAOType dt;
	
	public ConfigController(Result result,DAODatabaseStructure dbs,DAOProperty dp,DAOType dt)
	{
		this.result = result;
		this.dbs = dbs;
		this.dp = dp;
		this.dt = dt;
	}

	@Get
	@Path("/config/createDatabase")
	public void database()
	{
		this.dbs.create();
		this.result.include("result","OK!");
	}

	
	@Get
	@Path("/config/properties")
	public void properties()
	{
		
	}
	
	@Post
	@Path("/config/loadProperties")
	public void loadProperties(String data)
	{
		this.dp.deleteAll();
		
		String[] properties = data.split("\n");

		for(int i = 0 ; i < properties.length ;i ++)
		{
			EntityProperty ep = new EntityProperty();
			ep.setUri(properties[i].trim());	
			this.dp.insert(ep);
		}
		this.result.redirectTo(this).properties();
	}
	
	
	@Get
	@Path("/config/types")
	public void types()
	{	
	}
	
	@Post
	@Path("/config/loadTypes")
	public void loadTypes(String data)
	{
		this.dt.deleteAll();
		String[] properties = data.split("\n");

		for(int i = 0 ; i < properties.length ;i ++)
		{
			EntityType et = new EntityType();
			et.setUri(properties[i].trim());
			this.dt.insert(et);
		}
		
		this.result.redirectTo(this).types();
	}
	
	
	
}
